<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\admin\model\ThemeModel;
use app\portal\model\PortalDemoModel;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class AdminDemoController extends AdminBaseController
{
    /**
     * 案例列表
     * @return mixed
     * User: sunc
     * Date: 2021/6/22 10:29
     */
    public function index()
    {
        $list = Db::name('demo')->field('id,list_order,company_name,service_name,post_excerpt,thumb,published_time')->paginate(20);
        $this->assign('page', $list->render());//单独提取分页出来
        $this->assign('demo', $list);
        return $this->fetch();
    }

    /**
     * 添加文章
     * @return mixed
     * User: sunc
     * Date: 2021/6/22 10:25
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加文章提交
     *
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post = $data['post'];
            $portalDemoModel = new  PortalDemoModel();
            $post['post_content'] = htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($post['post_content']), true));
            $portalDemoModel->data($post)->save();
            $id = $portalDemoModel->id;
            $this->success('添加成功!', url('AdminDemo/edit', ['id' => $id]));
        }

    }

    /**
     * 编辑文章
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');

        $portalPostModel = new PortalDemoModel();
        $post = $portalPostModel->where('id', $id)->find();
        if (empty($post)) {
            $this->error('数据不存在');
        }
        $post['post_content'] = cmf_replace_content_file_url(htmlspecialchars_decode($post['post_content']));
        $this->assign('post', $post);
        return $this->fetch();
    }

    /**
     * 编辑文章提交
     *
     * @throws \think\Exception
     */
    public function editPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post = $data['post'];
            $post['post_content'] = htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($post['post_content']), true));
            $portalPostModel = new PortalDemoModel();
            $portalPostModel->update($post);
            $this->success('保存成功!');

        }
    }

    /**
     * 文章删除
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function delete()
    {
        $param = $this->request->param();
        $portalPostModel = new PortalDemoModel();

        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            $result = $portalPostModel->where('id', $id)->find();
            $result->delete();
            $this->success("删除成功！", '');
        }
        if (isset($param['ids'])) {
            $ids = $this->request->param('ids/a');
            $portalPostModel->where('id', 'in', $ids)->delete();
            $this->success("删除成功！", '');
        }
    }



    /**
     * 文章排序
     * @adminMenu(
     *     'name'   => '文章排序',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章排序',
     *     'param'  => ''
     * )
     */
    public function listOrder()
    {
        parent::listOrders('demo');
        $this->success("排序更新成功！", '');
    }
}
